import 'dart:math';

import 'package:flutter/material.dart';


const DefaultCircleProgressGradient =
 LinearGradient(
  colors: [Color(0xFFF64949), Color(0xFF9F045C)],
  begin: FractionalOffset(1, 0),
  end: FractionalOffset(0, 1),
);

class CircleProgressPointer extends CustomPainter {
  final double radius;
  final double borderWidth;
  final Color borderColor;
  final PaintingStyle borderStyle;
  final Gradient gradient;
  Color shadowColor;
  final double shadowWidth;
  final double progress;

  CircleProgressPointer({ 
    this.borderWidth = 2, 
    this.borderColor = Colors.black, 
    this.borderStyle = PaintingStyle.stroke, 
    this.radius = 25,
    this.gradient = DefaultCircleProgressGradient,
    this.shadowColor,
    this.shadowWidth = 1, 
    this.progress,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final offsetCenter = Offset(radius, radius);
    final Rect arcRect = Rect.fromCircle(center: offsetCenter, radius: radius);
    final borderPaint = Paint()
      ..color = borderColor
      ..style = borderStyle
      ..strokeWidth = borderWidth
      ..shader = gradient.createShader(arcRect);
    final double r = radius - borderWidth / 2;
    final double sweepAngle = progress * pi *2;

    canvas.drawArc(
      Rect.fromCenter(center: offsetCenter, width:  r*2, height: r*2), 0, sweepAngle, false, borderPaint
    );
  }
  
  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

class CYCircleProgress extends StatelessWidget {
  final Widget child;
  final double radius;
  final EdgeInsetsGeometry margin;
  final double progress;

  const CYCircleProgress({Key key, this.child, this.radius = 27, this.margin, this.progress = 0}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return 
    Container(
      margin: margin,
      child: Stack(
          alignment: AlignmentDirectional.center,
          children: <Widget>[
            Container(
              width: radius*2,
              height: radius*2,
              child: Transform.rotate(
                angle: - pi / 2,
                child: CustomPaint(
                  size: Size(radius, radius),
                  painter: CircleProgressPointer(
                    radius: radius, 
                    borderWidth: 2,
                    progress: progress,
                  ),
                )
              ),
            ),
            child,
          ],
      )
    );
  }
}